﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CUREBusinessLogicLayer;
using System.Web.SessionState;
using System.Text.RegularExpressions;
using CUREDataAccessLayers;
using CUREBusinessLogicLayer.Harmful;

namespace CURE.Handler.Harmful
{
    /// <summary>
    /// 不良胶F卡台账
    /// </summary>
    public class LedgerHandler : IHttpHandler, IRequiresSessionState
    {

        #region 变量
        public string id; public string Id { get { return id; } set { id = value; } }//流水号（主键）
        public string fid; public string FID { get { return fid; } set { fid = value; } }//F卡号
        public string deviceNo; public string DeviceNo { get { return deviceNo; } set { deviceNo = value; } }//设备编号
        public string spec; public string Spec { get { return spec; } set { spec = value; } }//规格
        public string proMonth; public string ProMonth { get { return proMonth; } set { proMonth = value; } }//生产月份

        public string proDay; public string ProDay { get { return proDay; } set { proDay = value; } }//生产日期
        public string orderBatch; public string OrderBatch { get { return orderBatch; } set { orderBatch = value; } }//指令批次号
        public string trayNo; public string TrayNo { get { return trayNo; } set { trayNo = value; } }//橡胶托盘编号
        public string trayBatch; public string TrayBatch { get { return trayBatch; } set { trayBatch = value; } }//橡胶批次
        public string expireMonth; public string ExpireMonth { get { return expireMonth; } set { expireMonth = value; } }//过期的月份

        public string expireDay; public string ExpireDay { get { return expireDay; } set { expireDay = value; } }//过期天数
        public string proTime; public string ProTime { get { return proTime; } set { proTime = value; } }//生产时间
        public string weight; public string Weight { get { return weight; } set { weight = value; } }//重量 
        public string reason; public string Reason { get { return reason; } set { reason = value; } }//原因
        public string fClass; public string FClass { get { return fClass; } set { fClass = value; } }//班别

        public string machine; public string Machine { get { return machine; } set { machine = value; } }//机台
        public string material; public string Material { get { return material; } set { material = value; } }//部材名称
        public string filmModel; public string FilmModel { get { return filmModel; } set { filmModel = value; } }//胶片型号
        public string oilModel; public string OilModel { get { return oilModel; } set { oilModel = value; } }//油型号
        public string actualNum; public string ActualNum { get { return actualNum; } set { actualNum = value; } }//实际保留数量

        public string bTNo; public string BTNo { get { return bTNo; } set { bTNo = value; } }//实际保留BT数
        public string bJBTNO; public string BJBTNO { get { return bJBTNO; } set { bJBTNO = value; } }//lot report报警BT数
        public string okMan; public string OkMan { get { return okMan; } set { okMan = value; } }//确认者
        public string field; public string Field { get { return field; } set { field = value; } }//保留置场
        public string makeMan; public string MakeMan { get { return makeMan; } set { makeMan = value; } }//制造确认者

        public string skiller; public string Skiller { get { return skiller; } set { skiller = value; } }//技术确认者
        public string handlerTime; public string HandlerTime { get { return handlerTime; } set { handlerTime = value; } }//处理日期
        public string fClass2; public string FClass2 { get { return fClass2; } set { fClass2 = value; } }//班别/直
        public string machine2; public string Machine2 { get { return machine2; } set { machine2 = value; } }//处理机台
        public string subModel; public string SubModel { get { return subModel; } set { subModel = value; } }//混入主体型号

        public string subBTNum; public string SubBTNum { get { return subBTNum; } set { subBTNum = value; } }//混入主体BT数
        public string interfuse; public string Interfuse { get { return interfuse; } set { interfuse = value; } }//混入量
        public string residual; public string Residual { get { return residual; } set { residual = value; } }//有无残留
        public string remarks; public string Remarks { get { return remarks; } set { remarks = value; } }//备注
        private string action;public string Action{get { return action; } set { action = value; } }
        private string page;

        public string Page
        {
            get { return page; }
            set { page = value; }
        }
        #endregion

        private LedgerBll ledgerbll = new LedgerBll();

        public void ProcessRequest(HttpContext context)
        {
            #region 变量
            Action = context.Request.Form["action"];
            Page = context.Request.Form["page"];
            Id = context.Request.Form["id"];
            FID = context.Request.Form["fid"];
            DeviceNo = context.Request.Form["deviceNo"];
            Spec = context.Request.Form["Spec"];
            ProMonth = context.Request.Form["ProMonth"];
            ProDay = context.Request.Form["ProDay"];
            OrderBatch = context.Request.Form["OrderBatch"];
            TrayNo = context.Request.Form["TrayNo"];
            TrayBatch = context.Request.Form["TrayBatch"];
            ExpireMonth = context.Request.Form["ExpireMonth"];
            ExpireDay = context.Request.Form["ExpireDay"];
            ProTime = context.Request.Form["ProTime"];
            Weight = context.Request.Form["Weight"];
            Reason = context.Request.Form["Reason"];
            FClass = context.Request.Form["FClass"];
            Machine = context.Request.Form["Machine"];
            Material = context.Request.Form["Material"];
            FilmModel = context.Request.Form["FilmModel"];
            OilModel = context.Request.Form["OilModel"];
            ActualNum = context.Request.Form["ActualNum"];
            BTNo = context.Request.Form["BTNo"];
            BJBTNO = context.Request.Form["BJBTNO"];
            OkMan = context.Request.Form["OkMan"];
            Field = context.Request.Form["Field"];
            MakeMan = context.Request.Form["MakeMan"];
            Skiller = context.Request.Form["Skiller"];
            HandlerTime = context.Request.Form["HandlerTime"];
            FClass2 = context.Request.Form["FClass2"];
            Machine2 = context.Request.Form["Machine2"];
            SubModel = context.Request.Form["SubModel"];
            SubBTNum = context.Request.Form["SubBTNum"];
            Interfuse = context.Request.Form["Interfuse"];
            Residual = context.Request.Form["Residual"];
            Remarks = context.Request.Form["Remarks"];

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("mid", Id);
            dic.Add("DeviceNo", DeviceNo);
            dic.Add("Spec", Spec);
            dic.Add("ProMonth", ProMonth);
            dic.Add("ProDay", ProDay);
            dic.Add("OrderBatch", OrderBatch);
            dic.Add("TrayNo", TrayNo);
            dic.Add("TrayBatch", TrayBatch);
            dic.Add("ExpireMonth", ExpireMonth);
            dic.Add("ExpireDay", ExpireDay);
            dic.Add("ProTime", ProTime);
            dic.Add("Weight", Weight);
            dic.Add("Reason", Reason);
            dic.Add("FClass", FClass);
            dic.Add("Machine", Machine);
            dic.Add("Material", Material);
            dic.Add("FilmModel", FilmModel);
            dic.Add("OilModel", OilModel);
            dic.Add("ActualNum", ActualNum);
            dic.Add("BTNo", BTNo);
            dic.Add("BJBTNO", BJBTNO);
            dic.Add("OkMan", OkMan);
            dic.Add("Field", Field);
            dic.Add("MakeMan", MakeMan);
            dic.Add("Skiller", Skiller);
            dic.Add("HandlerTime", HandlerTime);
            dic.Add("FClass2", FClass2);
            dic.Add("Machine2", Machine2);
            dic.Add("SubModel", SubModel);
            dic.Add("SubBTNum", SubBTNum);
            dic.Add("Interfuse", Interfuse);
            dic.Add("Residual", Residual);
            dic.Add("Remarks", Remarks);
            dic.Add("FID", FID);
            dic.Add("page", Page);
            #endregion
            if (Action == "edit")
            {
                if (UpdateLedgerInfo(context, dic))
                {

                    context.Response.Write(success("Harmful/" + Page + ".aspx"));
 
                }
                else
                {
                    context.Response.Write(failure());
                }
            }
            else if (Action == "add")
            {
                if (AddLedgerInfo(context, dic))
                {
                    context.Response.Write(success("Harmful/" + Page + ".aspx"));
                }
                else
                {
                    context.Response.Write(failure());
                }
            }
            else
            {
                if (DeleteLedgerInfo(context))
                {
                    context.Response.Write(success1());
                }
                else
                {
                    context.Response.Write(failure());
                }
            }
        }

        /// <summary>
        /// 更新台账数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        private bool UpdateLedgerInfo(HttpContext context, Dictionary<string, object> dic)
        {
            return ledgerbll.UpdateLedgerInfo(dic);
        }

        /// <summary>
        /// 添加台账数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        private bool AddLedgerInfo(HttpContext context, Dictionary<string, object> dic)
        {
            return ledgerbll.AddLedgerInfo(dic);
        }

        /// <summary>
        /// 删除台账数据
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private bool DeleteLedgerInfo(HttpContext context)
        {
            return ledgerbll.ChangeLedgerState(context.Request.QueryString["id"],-1);
        }


        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"navTabId\":\"editLedger\", \"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string successfoward(string str, string url)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"forward\",\"forwardUrl\":\""+url+"\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}